﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using zj.Communicationlib;
using zj.Communicationlib.ByteArrays;
using zj.Communicationlib.Modbus;
using System.IO.Ports;
namespace test
{
    internal class Program
    {
        private static  ModbusRTU modbusRTU = new ModbusRTU();
        static void Main(string[] args)
        {
            
           // TestModbusRTU_08_5();
            //byte[] bytes =  ByteArrayHelper.GetByteArray<double >(15.6d , s=> BitConverter.GetBytes(s).Reverse().ToArray());


        }
        /// <summary>
        /// 测试一号功能
        /// </summary>
        private static void TestModbusRTU_01 ()
        {
            if(modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                bool[] bools= modbusRTU.ReadMultiCoil(0x01, RegisterSaveType.Hold, 0x01, 20);
                
            }
            modbusRTU.DisConnect();
        }
        private static void TestModbusRTU_02()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                bool[] bools = modbusRTU.ReadMultiCoil(0x01, RegisterSaveType.Input, 0x01, 20);

            }
            modbusRTU.DisConnect();
        }
        private static void TestModbusRTU_03()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                byte[] bytes = modbusRTU.ReadMultiRegister(0x01, RegisterSaveType.Hold, 0x00, 20);

            }
            modbusRTU.DisConnect();
        }
        private static void TestModbusRTU_04()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                byte[] bytes = modbusRTU.ReadMultiRegister(0x01, RegisterSaveType.Input, 0x00, 20);

            }
            modbusRTU.DisConnect();
        }
        private static void TestModbusRTU_05()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                modbusRTU.WriteSingleCoil(0x01,23,false);

            }
            modbusRTU.DisConnect();
        }
        private static void TestModbusRTU_06_1()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                modbusRTU.WriteSingleRegister(0x01, 6, 125,false);

            }
            modbusRTU.DisConnect();
        }

        private static void TestModbusRTU_06_2()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                modbusRTU.WriteSingleRegister(0x01, 7, new byte[]{ 0x12,0x34});

            }
            modbusRTU.DisConnect();
        }
        private static void TestModbusRTU_07()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                bool b = modbusRTU.WriteMultiCoils(0x01, 0, new bool[] { false ,false,false , true , true });

            }
            modbusRTU.DisConnect();
        }

        private static void TestModbusRTU_08_1()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                bool b = modbusRTU.WriteMultiRegister(0x01, 0, new byte[] { 0x08,0x09,0x10,0x11});

            }
            modbusRTU.DisConnect();
        }
        private static void TestModbusRTU_08_2()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                bool b = modbusRTU.WriteMultiRegister(0x01, 1,new ushort[] { 325,664,686},false);

            }
            modbusRTU.DisConnect();
        }
        private static void TestModbusRTU_08_3()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                bool b = modbusRTU.WriteMultiRegister(0x01, 1, new short[] { -325, 664, -686 },false);

            }
            modbusRTU.DisConnect();
        }
        //private static void TestModbusRTU_08_4()
        //{
        //    if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
        //    {
        //        bool b = modbusRTU.WriteMultiRegister(0x01, 1, new uint[] { 32767, 66400, 68678 },RegisterFormat.ABCD);

        //    }
        //    modbusRTU.DisConnect();
        //}
        //private static void TestModbusRTU_08_5()
        //{
        //    if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
        //    {
        //        bool b = modbusRTU.WriteMultiRegister(0x01, 1, new int[] { 767, 66400, 678 }, RegisterFormat.ABCD);

        //    }
        //    modbusRTU.DisConnect();
        //}
        private static void TestModbusRTU_08_5()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                bool b = modbusRTU.WriteMultiRegister(0x01, 1, new long[] { 1311768467463790321, 62623859790382856, 678 }, RegisterFormat.ABCD);

            }
            modbusRTU.DisConnect();
        }
        private static void TestModbusRTU_08_6()
        {
            if (modbusRTU.Connect("COM20", Boundrate.BR_9600, Parity.None, Databits.bit_8, StopBits.One))
            {
                bool b = modbusRTU.WriteMultiRegister<long>(0x01, 1, new long[] { 1311768467463790321, 62623859790382856, 678 },s=>BitConverter.GetBytes(s).Reverse().ToArray(), RegisterFormat.ABCD);

            }
            modbusRTU.DisConnect();
        }
    }
}
